package com.xmad.ksadutils.AdSDK.AdMold.OnCallback;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdDislike;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.xmad.ksadutils.AdSDK.AdBase.TTAdManagerHolder;
import com.xmad.ksadutils.AdSDK.Interface.AdOnLoad;
import com.xmad.ksadutils.AdSDK.Utlis.RxLogUtils;
import com.xmad.ksadutils.AdSDK.Utlis.Tool;

import java.util.List;

public class PGloadNativeExpressAd implements AdOnLoad {
    private volatile static PGloadNativeExpressAd mInstance;
    /**
     * 单例模式
     *
     * @return
     */
    public static PGloadNativeExpressAd getInstance() {
        if (mInstance == null) {
            synchronized (PGloadNativeExpressAd.class) {
                if (mInstance == null) {
                    mInstance = new PGloadNativeExpressAd();
                }
            }
        }
        return mInstance;
    }

    private void AdListener(TTNativeExpressAd ad, final FrameLayout frameLayout, Activity context) {
        ad.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
            @Override
            public void onAdClicked(View view, int type) {
                Log.e("hjw","广告被点击");

            }

            @Override
            public void onAdShow(View view, int type) {
                Log.e("onAdShow","onAdShow");
                frameLayout.removeAllViews();
            }

            @Override
            public void onRenderFail(View view, String msg, int code) {
                Log.d("hjw", msg+" code:"+code);
            }

            @Override
            public void onRenderSuccess(View view, float width, float height) {
               // if (ActivityForeground.isForeground(context)){
                    Log.e("暂停暂停","暂停暂停");
                    frameLayout.removeAllViews();
              //      return;
              //  }
                //返回view的宽高 单位 dp
                TTNativeExpressAd ttNativeExpressAd;
                if (frameLayout!=null){
                    frameLayout.removeAllViews();
//                    FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) view.getLayoutParams();
////                    // 设置里面的控件的布局和父布局的高度相同
//                    layoutParams.height = FrameLayout.LayoutParams.MATCH_PARENT;
//                    view.setLayoutParams(layoutParams);
////                    // 设置控件自身的内容居中
                   // view.setBackgroundColor(context.getColor(R.color.black));
                    frameLayout.addView(view);
                    frameLayout.setPadding(5,5,5,5);
                }else {
                   RxLogUtils.e("没有添加布局");
                }
            }
        });
        //dislike设置
        bindDislike(ad, frameLayout,false,context);
        if (ad.getInteractionType() != TTAdConstant.INTERACTION_TYPE_DOWNLOAD){
            return;
        }
        ad.setDownloadListener(new TTAppDownloadListener() {
            @Override
            public void onIdle() {
                Log.d("hjw","点击开始下载");
            }

            @Override
            public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {

            }

            @Override
            public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
                Log.d("hjw","下载暂停，点击继续");
            }

            @Override
            public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
                Log.d("hjw","下载失败，点击重新下载");
            }

            @Override
            public void onInstalled(String fileName, String appName) {
                Log.d("hjw","安装完成，点击图片打开");
            }

            @Override
            public void onDownloadFinished(long totalBytes, String fileName, String appName) {
                Log.d("hjw","点击安装");
            }
        });
    }

    /**
     * 设置广告的不喜欢，注意：强烈建议设置该逻辑，如果不设置dislike处理逻辑，则模板广告中的 dislike区域不响应dislike事件。
     * @param ad
     * @param customStyle 是否自定义样式，true:样式自定义
     */
    private void bindDislike(TTNativeExpressAd ad, final FrameLayout frameLayout, boolean customStyle, Activity context) {
        //  使用默认模板中默认dislike弹出样式
        ad.setDislikeCallback(context, new TTAdDislike.DislikeInteractionCallback() {
            @Override
            public void onShow() {

            }

            @Override
            public void onSelected(int i, String s, boolean b) {
                Log.d("TAGxx", "点击 " +  s);
                //用户选择不喜欢原因后，移除广告展示
                frameLayout.removeAllViews();
                frameLayout.setVisibility(View.GONE);
            }

            @Override
            public void onCancel() {
                Log.d("TAGxx", "点击取消 ");
            }

        });
    }

    @Override
    public void noCallback(Long id, Activity activity, FrameLayout frameLayout) {

    }

    @Override
    public void noCallback(String id, Activity activity, FrameLayout frameLayout) {
        TTAdNative TTAdNative = TTAdManagerHolder.get().createAdNative(activity);
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(id)
                .setSupportDeepLink(true)
                .setAdCount(1) //请求广告数量为1到3条
                .setExpressViewAcceptedSize(Tool.px2dp(activity, Tool.getScreenWidth(activity))-20,0) //期望模板广告view的size,单位dp
                .build();
        //step5:请求广告，对请求回调的广告作渲染处理
        TTAdNative.loadNativeExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int code, String message) {
                Log.e("onError",code+message);
                frameLayout.removeAllViews();
            }
            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
                Log.e("111111111111111111","222222222222222222");
                if (ads == null || ads.size() == 0){
                    return;
                }
                TTNativeExpressAd   mTTAd = ads.get(0);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        AdListener(mTTAd,frameLayout,activity);
                        mTTAd.render();
                    }
                });
            }
        });
    }
}
